<?php

namespace App\Http\Controllers;
use App\Comment;
use App\Http\Requests\PostCreateRequest;
use App\Post;
use App\Zan;
use Illuminate\Http\Request;

class PostController extends Controller
{
    protected $fields = [
        'title' => '',
        'content' => ''
    ];
    public function index(){
        $posts = Post::orderBy('created_at','desc')
            ->with(['user','comments','zans'])
            ->withCount(['comments','zans'])
            ->paginate(6);
//        $posts = Post::all();
//        foreach($posts as $post){
//            $counts = $this->countComments($post->id);
//            $post->counts =$counts;
//        }
//        foreach($posts as $post){
//           echo $post->id.'---'.$post->counts.'<br/>' ;
//        }
//        dd();
        return view('post.index',compact('posts'));
    }
    public function create(){
        return view('post.create');
    }
    public function store(PostCreateRequest $request){
        $post = new Post();
        foreach (array_keys($this->fields) as $field){
            $post->$field = $request->get($field);
        }
        $post->user_id = \Auth::id();
        $post->save();
        return redirect('/post');
    }
    public function show($id){
       $post = Post::find($id);
       $comments = Comment::with('user')->where('post_id',$id)->get();
        return view('post.show',compact('post','comments'));
    }
    public function  edit($id){
        $post = Post::find($id);
        return view('post.edit',compact('post'));
    }
    public function  update(Request $request,$id){
        $post = Post::findOrFail($id);
        foreach (array_keys($this->fields) as $field){
            $post->$field = $request->$field;
        }
        $this->authorize('update', $post);
        $post->save();
        return redirect("/post/$post->id");
    }
    public  function  delete($id){
        $post = Post::findOrFail($id);
        $this->authorize('delete', $post);
        $post->delete();
        return redirect("/post");
    }
    public function  imageUpload(Request $request){
        $path = $request->file('wangEditorH5File')->storePublicly(md5(time()));
        return asset('storage/'. $path);
    }

    public function comment(Request $request){
        $comment = new Comment();
        $comment->post_id = $request->get('post_id');
        $comment->content = request('content');
        $comment->user_id = \Auth::id();
        $comment->save();
        return back();
    }

    public function countComments($id){
        $count = Comment::where('post_id',$id)->count();
        return $count;
    }

    public function zan($post_id) {
        $params = [
            'user_id' => \Auth::id(),
            'post_id' => $post_id
        ];
        Zan::firstOrCreate($params);
        return back();
    }

    public function unZan($post_id){
       $zan = Zan::where('post_id',$post_id)->where('user_id',\Auth::id())->firstOrFail();
       //$post = Post::find($post_id);
       $zan->delete();
        return back();
    }
}
